﻿XIncludeFile "GDI+.pb"

UsePNGImageDecoder()

#GD_TextAlignCenter=1  
#GD_TextAlignRight =2
#GD_TextLineAlignCenter =4
#GD_TextLineAlignDown  =8
#GD_TextFlagsVerticalLeft   =16
#GD_TextFlagsVerticalRight   =32

Procedure ARGB(Transparecy,RGB) 
  ProcedureReturn Blue(RGB)|Green(RGB)<<8|Red(RGB)<<16|Transparecy<<24 
EndProcedure 
;读取指定颜色中的透明分量
Procedure Alphaa(Color) 
!MOVZX Dword eax,Byte [esp+7] 
!RET 4
EndProcedure

;-> 判断数据取值
Procedure GetVal(Val,IFVal)  
 al=(Val ! IFVal) | IFVal
 If Val= al
   ProcedureReturn #True
 EndIf 
EndProcedure

Procedure GdipLoadImage(Path.s)
  GdipCreateBitmapFromFile(Path,@Image)
  GdipCloneImage(Image,@New)
  GdipDisposeImage(Image)
  ProcedureReturn New
EndProcedure

Procedure GdipCreateImage(Width,Height)
  GdipCreateBitmapFromScan0(Width,Height,0,#GDIPLUS_PIXELFORMAT_32bppARGB, 0,@Image)
  ProcedureReturn Image
EndProcedure

Procedure GdipCatchImage(Address, Length)
CompilerIf Defined(StreamObject , #PB_Structure) = 0 
  Structure StreamObject 
    block.l 
    *bits 
    stream.ISTREAM 
  EndStructure 
CompilerEndIf  
   stream.StreamObject  
   Stream\block = GlobalAlloc_(#GHND, Length) 
   Stream\bits = GlobalLock_(Stream\block) 
   CopyMemory(address, stream\bits, Length) 
   If CreateStreamOnHGlobal_(stream\bits, 0, @Stream\stream) = #S_OK 
      GdipCreateBitmapFromStream( Stream\stream , @*image) 
      Stream\stream\Release() 
      GlobalUnlock_(Stream\bits) 
      GlobalFree_(Stream\block) 
      

      GdipGetImageWidth(*Image,@Width)
      GdipGetImageHeight(*Image,@Height)
      GdipCreateBitmapFromScan0(Width,Height,0,#GDIPLUS_PIXELFORMAT_32bppARGB, 0,@Image)
      
      GdipGetImageGraphicsContext(Image,@Graphics)
       GdipSetCompositingMode(Graphics,#CompositingModeSourceCopy)
       GdipDrawImageRectI(Graphics,*image,0,0,Width,Height)
      GdipDeleteGraphics(Graphics)
      
      GdipDisposeImage( *image)
      ProcedureReturn Image
   EndIf 
EndProcedure

Procedure GdipCopyImage(Image)
  GdipCloneImage(Image,@New)
ProcedureReturn New
EndProcedure

Procedure GdipImageWidth(Image)
  GdipGetImageWidth(Image,@Width)
  ProcedureReturn Width
EndProcedure

Procedure GdipImageHeight(Image)
  GdipGetImageHeight(Image,@Height)
  ProcedureReturn Height  
EndProcedure

Procedure GdipImageDraw(Garphics,Image,X,Y)
  GdipGetImageWidth (Image,@Width )
  GdipGetImageHeight(Image,@Height)
  GdipDrawImageRectI(Garphics,Image,X,Y,Width,Height)
EndProcedure

Procedure GdipImageTile(Graphics,Image,X,Y,Width,Height)
  GdipCreateBitmapFromScan0(Width,Height,0,#GDIPLUS_PIXELFORMAT_32bppARGB, 0,@NewImage)
  GdipGetImageGraphicsContext(NewImage,@NewGraphics) 
  
  GdipCreateTexture(Image,#WrapModeTile,@Brush)
  GdipFillRectangle(NewGraphics,Brush,0,0,Width,Height)
  GdipDrawImageRectI(Graphics,NewImage,X,Y,Width,Height)
  GdipDeleteGraphics(NewGraphics)
  
  GdipDisposeImage(NewImage)
  GdipDeleteBrush(Brush)
  ProcedureReturn  Image
EndProcedure

Procedure GdipDrawText(Graphics,Text.s,X,Y,Width,Height,FontName.s,FontWSize.f,FontStyle,Color,Align=0,Style=1)

  GdipCreateFontFamilyFromName(FontName,#Null,@Family)  ;创建字体名
  GdipCreateFont(Family,FontWSize,FontStyle,2,@Font)    ;创建字体

   
  GdipCreateSolidFill(Color,@Brush)                     ;创建纯色笔刷
  GdipCreateStringFormat(0, 0, @StrFormat)             ;创建字符格式
  
  If GetVal(Align,1)
   Rank=1
  ElseIf GetVal(Align,2)
   Rank=2
  EndIf 
  GdipSetStringFormatAlign(strFormat,Rank)             ;设置字符横向对齐格式
  If GetVal(Align,4)
    Row=1
  ElseIf GetVal(Align,8)
    Row=2
  EndIf 
  GdipSetStringFormatLineAlign(strFormat, Row)            ;设置字符垂直对齐格式
  If GetVal(Align,16)
    Dire=2
  ElseIf GetVal(Align,32)
    Dire=3
  EndIf
  GdipSetStringFormatFlags(strFormat, Dire)                ;设置字符方向
  
  Rect.Gdip_RectF 
  Rect\left   =X
  Rect\top    =Y 
  Rect\Width  =Width 
  Rect\Height =Height

  GdipSetTextRenderingHint(Graphics,Style)           ;设置文字抗字体类型
  GdipDrawString(Graphics,Text,-1,Font,Rect,StrFormat,Brush)   ;画出字符

  GdipDeleteStringFormat(StrFormat)                     ;删除字符格式
  GdipDeleteBrush(Brush)                                ;销毁纯色
  GdipDeleteFont(Font)                                  ;销毁字体
  GdipDeleteFontFamily(Family)                          ;销毁字体名

EndProcedure

Procedure GdipPathText(Graphics,Text.s,X,Y,Width,Height,FontName.s,FontWSize.f,FontStyle,TextColor,PenColor,PenWidth.f=1,Align=0,Style=1)
 
  GdipCreateFontFamilyFromName(FontName,#Null,@Family)  ;创建字体名
  GdipCreateStringFormat(0, 0, @StrFormat)             ;创建字符格式
  
  
  If GetVal(Align,1)
   Rank=1
  ElseIf GetVal(Align,2)
   Rank=2
  EndIf 
  GdipSetStringFormatAlign(strFormat,Rank)             ;设置字符横向对齐格式
  If GetVal(Align,4)
    Row=1
  ElseIf GetVal(Align,8)
    Row=2
  EndIf 
  GdipSetStringFormatLineAlign(strFormat, Row)            ;设置字符垂直对齐格式
  If GetVal(Align,16)
    Dire=2
  ElseIf GetVal(Align,32)
    Dire=3
  EndIf
  GdipSetStringFormatFlags(strFormat, Dire)                ;设置字符方向
  GdipCreateSolidFill(TextColor,@Brush)                 ;创建纯色笔刷       
  GdipCreatePen1(PenColor, PenWidth, 2,  @Pen)          ;创建刚笔
  GdipCreatePath(0, @StrPath)                           ;创建路径

  Rect.Gdip_RectF 
  Rect\left   =X
  Rect\top    =Y 
  Rect\Width  =Width 
  Rect\Height =Height    
  GdipAddPathString(StrPath,Text, -1, Family,FontStyle,FontWSize, Rect, StrFormat)  ;读取字符路径                  

  GdipSetSmoothingMode(Graphics, Style)              ;设置绘制模式
  GdipFillPath(Graphics, Brush, StrPath)             ;填充路径
  If PenWidth
  GdipDrawPath(Graphics, Pen, StrPath)               ;绘制路径
  EndIf 
  GdipDeletePath(StrPath)                               ;删除路径
  GdipDeletePen(Pen)                                    ;删除钢笔
  GdipDeleteBrush(Brush)                                ;删除笔刷
  GdipDeleteStringFormat(StrFormat)                     ;删除字符格式
  GdipDeleteFontFamily(Family)                          ;删除字体名
 
EndProcedure

Procedure GdipImageText(Graphics,Text.s,X,Y,Width,Height,FontName.s,FontWSize.f,FontStyle,TextColor,PenColor,PenWidth.f=1,Align=0,Style=1)
  If PenWidth
    GdipPathText(Graphics,Text.s,X,Y,Width,Height,FontName.s,FontWSize.f,FontStyle,TextColor,PenColor,PenWidth.f,Align,Style)
  Else
    GdipDrawText(Graphics,Text.s,X,Y,Width,Height,FontName.s,FontWSize.f,FontStyle,TextColor,Align,Style)
  EndIf 
EndProcedure
; IDE Options = PureBasic 4.40 (Windows - x86)
; CursorPosition = 15
; FirstLine = 13
; Folding = 7N-
; EnableAsm
; EnableUnicode
; EnableXP
; HideErrorLog
; CompileSourceDirectory